package com.huluobo.a2204kotlinshopping.setting

import androidx.lifecycle.MutableLiveData
import com.huluobo.base.bean.CommonBeanResult
import com.huluobo.base.bean.UserBean
import com.huluobo.base.net.Const
import com.huluobo.base.viewmodel.BaseViewModel
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 *  Created by LC on 2024/8/6.
 */
class SettingViewModel : BaseViewModel<SettingRepo>() {

    val updateIconSuccess = MutableLiveData<CommonBeanResult>()
    val updateIconFailed = MutableLiveData<String>()

    val queryUserInfoSuccess = MutableLiveData<UserBean>()
    val queryUserInfoFailed = MutableLiveData<String>()

    val updateNicknameSuccess = MutableLiveData<CommonBeanResult>()
    val updateNicknameFailed = MutableLiveData<String>()

    val updateBirthSuccess = MutableLiveData<CommonBeanResult>()
    val updateBirthFailed = MutableLiveData<String>()

    val updateSexSuccess = MutableLiveData<CommonBeanResult>()
    val updateSexFailed = MutableLiveData<String>()

    fun updateNickname(nickname: String) {
        repo.updateNickname(nickname, updateNicknameSuccess, updateNicknameFailed)
    }

    fun updateBirth(birth: String) {
        repo.updateBirth(birth, updateBirthSuccess, updateBirthFailed)
    }

    fun updateSex(sex: String) {
        repo.updateSex(sex, updateSexSuccess, updateSexFailed)
    }

    fun queryUserInfo(username: String) {
        repo.queryUserInfo(username, queryUserInfoSuccess, queryUserInfoFailed)
    }

    fun updateIcon(filePath: String) {
        //1.创建文件对象
        val file = File(filePath)
        //2.封装body
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), file)
        //3.封装为part
        val part = MultipartBody.Part.createFormData(Const.PARAM_FILE, file.name, body)
        repo.updateIcon(part, updateIconSuccess, updateIconFailed)
    }

    override fun createRepo(): SettingRepo = SettingRepo()
}